<?php
/**
 * 用户私信表
 * @author yangchao
 * @package user
 * @copyright yangchao
 * @version 1.0
 * @code-encode utf-8
 * @data-encode 2011-01-17
 */

class db_user_message extends db_base {
    
	var $table_name = "user_message";
	
	function db_user_message()
	{
		parent::db_base();
	}
	
	
	//根据uid取个数
	function get_count_by_uid($uid)
	{       
		$uid = intval($uid);
		$where = " (from_uid = {$uid} and from_status = 1 and from_last = 1) or (to_uid = {$uid} and to_status =1 and to_last = 1)" ;	
		
		$data = $this->get_listcount($this->table_name, $where);

		return $data;
	}	
	
	
	//根据uid取列表
	function get_list_by_uid($uid,$p=1,$pcount=20)
	{       
		$uid = intval($uid);
		$p = intval($p);
		$pcount = intval($pcount);
		
		//处理where条件
		$where = " (from_uid = {$uid} and from_status = 1 and from_last = 1) or (to_uid = {$uid} and to_status =1 and to_last = 1)" ;	
	
		$data = $this->get_alllist($this->table_name, $p, $pcount, "create_time desc ",$where);

		return $data;
	}	
	

	//根据uid取个数
	function get_count_by_uid_detail($from_uid,$to_uid)
	{       
		$from_uid = intval($from_uid);
		$to_uid = intval($to_uid);
		
		
		$where = " (from_uid = {$from_uid} and to_uid = $to_uid and from_status = 1 ) or (from_uid = {$to_uid} and to_uid = $from_uid and to_status =1 )" ;	
		
		$data = $this->get_listcount($this->table_name, $where);

		return $data;
	}	
	
	
	//根据uid取列表
	function get_list_by_uid_detail($from_uid,$to_uid,$p=1,$pcount=20)
	{       
		$from_uid = intval($from_uid);
		$to_uid = intval($to_uid);
		
		$p = intval($p);
		$pcount = intval($pcount);
		
		
		//处理where条件
		$where = " (from_uid = {$from_uid} and to_uid = $to_uid and from_status = 1 ) or (from_uid = {$to_uid} and to_uid = $from_uid and to_status =1 )" ;	
		
		$data = $this->get_alllist($this->table_name, $p, $pcount, "create_time desc ",$where);

		return $data;
	}	
	
	
	function get_user_unread_count($to_uid,$create_time)
	{
		$create_time = mysql_escape_string($create_time);
		$to_uid = intval($to_uid);
		
		
		
		$where = "to_uid = {$to_uid} and create_time > '{$create_time}'  ";
		
		$data = $this->get_listcount($this->table_name, $where);

		return $data;
	
	}
	
	//判断当前用户一定时间内发了多少条
	function get_user_have_send_count($from_uid,$create_time)
	{
		$create_time = mysql_escape_string($create_time);
		$from_uid = intval($from_uid);
		
		$where = "from_uid = {$from_uid} and create_time > '{$create_time}'  ";
		
		$data = $this->get_listcount($this->table_name, $where);

		return $data;
	
	}
	
	function get_uid_by_from_uid($from_uid)
	{
		$from_uid = intval($from_uid);
		
		$sql = "select distinct(to_uid) as uid from user_message where from_uid = $from_uid  ";
		$res = $this->select($sql);
		return $res;
		
	}
	
	function get_max_mid_by_to_uid($to_uid)
	{
		$to_uid = intval($to_uid);
		
		$sql = "select max(mid) as mid from user_message where to_uid ={$to_uid} ";
		
		
		$res = $this->select($sql);
		return $res[0]["mid"]?$res[0]["mid"]:0;
	}
	
	function insert_user_message($from_uid,$to_uid,$msg_content,$mid="",$create_time="")
	{
		$ary["from_uid"] = $from_uid;
		$ary["to_uid"] = $to_uid;
		$ary["msg_content"] = $msg_content;
		$ary["mid"] = $mid;
		
		if($mid)
		{
			$ary["crc32_key"] = $mid;
		}
		else
		{
			$ary["crc32_key"] = crc32($from_uid.time());
		}
		
		$ary["create_uid"] = $from_uid;
		if($create_time)
		{
			$ary["create_time"] = $create_time;
		}
		else
		{
			$ary["create_time"] = Date::get_date_time();
		}
		$ary["create_ip"] = IP::get_client_ip_long();
		
		$ary["from_last"] = 1;
		$ary["to_last"] = 1;
		
		$ary["status"] = 1;
		
		$ret = $this->insert($this->table_name, $ary);
		
		return $ret;
	}
	
	
	function update_last_by_from_uid_and_to_uid($from_uid,$to_uid)
	{
		$from_uid = intval($from_uid);
		$to_uid = intval($to_uid);
		
		$where = "( from_uid = $from_uid and to_uid = $to_uid) or ( from_uid = $to_uid  and to_uid = $from_uid)";
		$ary["from_last"] = 0;
		$ary["to_last"] = 0;
		
		$res = $this->update($this->table_name,$ary,$where);
	}
	
	
	function get_count_by_from_uid_and_to_uid_no_read($from_uid,$to_uid)
	{
		$from_uid = intval($from_uid);
		$to_uid = intval($to_uid);
		
		$where = "from_uid = $from_uid and to_uid = $to_uid and to_have_read=0 and status=1";
		$count = $this->get_listcount($this->table_name,$where);
		return $count;
	}
	
	function update_to_have_read($from_uid,$to_uid)
	{
		$from_uid = intval($from_uid);
		$to_uid = intval($to_uid);
		
		$where = "from_uid = $from_uid and to_uid = $to_uid";
		$ary["to_have_read"] = 1;
		$res = $this->update($this->table_name,$ary,$where);
		return $res;
	}
	
	###########admin end
	
	
	
	
	
}



